package com.litesuits.sdk.views;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.litesuits.android.log.Log;
import com.litesuits.sdk.R;
import com.litesuits.sdk.act.LiteActivity;
import com.litesuits.sdk.act.LiteFragment;

/**
 * @author MaTianyu @http://litesuits.com
 * @date 2015-07-30
 */
public class DefaultTitleBar implements View.OnClickListener {
    private static final String TAG = DefaultTitleBar.class.getSimpleName();
    private View decorView;
    private LiteActivity activity;
    private ViewGroup rootView;
    /**
     * 左边图片(后退)
     */
    private ImageView ivLeft;
    /**
     * 右边图片
     */
    private ImageView ivRight;
    /**
     * 右边文字的情况
     */
    private TextView tvRight;
    /**
     * 中央标题内容
     */
    private TextView tvTitle;
    /**
     * 右边的LinearLayout
     */
    private LinearLayout ll_right;

    public DefaultTitleBar(LiteFragment fragment) {
        View view = fragment.getView();
        if (view != null) {
            this.decorView = view;
            this.activity = (LiteActivity) fragment.getActivity();
            init(view);
        }
    }

    public DefaultTitleBar(LiteActivity activity) {
        View view = activity.getWindow().getDecorView();
        if (view != null) {
            this.decorView = view;
            this.activity = activity;
            init(view);
        }
    }

    /**
     * 初始化标题
     */
    public void init(View view) {
        rootView = (ViewGroup) view.findViewById(R.id.titleBar);
        if (rootView != null) {
            ivLeft = (ImageView) rootView.findViewById(R.id.ivLeft);
            ivRight = (ImageView) rootView.findViewById(R.id.ivRight);
            tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
            tvRight = (TextView) rootView.findViewById(R.id.tvRight);
            ll_right = (LinearLayout)rootView.findViewById(R.id.ll_right);
            if (ivLeft != null) {
                ivLeft.setOnClickListener(this);
            }
        } else {
            Log.e(TAG, "TitleBar Rootview is null!!!");
        }
    }

    public boolean hasTitlebar() {
        return rootView != null;
    }

    @Override
    public void onClick(View v) {
        if (v == ivLeft) {
            if (activity != null) {
                activity.finish();
            }
        }
    }

    /**
     * 设置左边图片
     */
    public void setLeftImage(int image) {
        ivLeft.setImageResource(image);
        ivLeft.setVisibility(View.VISIBLE);
    }

    /**
     * 设置左边图片 消失
     */
    public void setLeftImageGone() {
        ivLeft.setVisibility(View.GONE);
    }

    /**
     * 设置右边图片 消失
     */
    public void setRightImageGone() {
        ivRight.setVisibility(View.GONE);
    }
    /**
     * 设置右边图片 显示
     */
    public void setRightImageVisible() {
        ivRight.setVisibility(View.VISIBLE);
    }
    /**
     * 设置右边的文字，并设置文字颜色
     */
    public void setRightTxt(String txt, int color) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setTextColor(activity.getResources().getColor(color));
        tvRight.setText(txt);
    }

    /**
     * 右边的文字,设置跳转方法
     */
    public void setRightTxtClick(View.OnClickListener view) {
        tvRight.setVisibility(View.VISIBLE);
        ll_right.setOnClickListener(view);
    }


    /**
     * 设置右边的文字的背景
     */
    public void setRightTxtBackground(int backgroundId) {
        tvRight.setBackgroundResource(backgroundId);
    }

    /**
     * 设置右边的文字
     */
    public void setRightTxt(String txt) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(txt);
    }

    /**
     * 设置右边的文字隐藏
     */
    public void setRightTxtGone() {
        tvRight.setVisibility(View.GONE);
    }

    /**
     * 设置右边的图片
     */
    public void setRightImage(int image) {
        ivRight.setImageResource(image);
        ivRight.setVisibility(View.VISIBLE);
    }
    /**
     * 右边的图片,设置跳转方法
     */
    public void setRightImageClick(View.OnClickListener view) {
        ivRight.setVisibility(View.VISIBLE);
        ll_right.setOnClickListener(view);
    }
    /**
     * 设置标题名称
     */
    public void changeTitle(String title) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(title);
    }

    public View getDecorView() {
        return decorView;
    }

    public DefaultTitleBar setDecorView(View decorView) {
        this.decorView = decorView;
        return this;
    }

    public LiteActivity getActivity() {
        return activity;
    }

    public DefaultTitleBar setActivity(LiteActivity activity) {
        this.activity = activity;
        return this;
    }

    public ViewGroup getRootView() {
        return rootView;
    }

    public DefaultTitleBar setRootView(ViewGroup rootView) {
        this.rootView = rootView;
        return this;
    }

    public ImageView getIvLeft() {
        return ivLeft;
    }

    public DefaultTitleBar setIvLeft(ImageView ivLeft) {
        this.ivLeft = ivLeft;
        return this;
    }

    public ImageView getIvRight() {
        return ivRight;
    }

    public DefaultTitleBar setIvRight(ImageView ivRight) {
        this.ivRight = ivRight;
        return this;
    }

    public TextView getTvRight() {
        return tvRight;
    }

    public DefaultTitleBar setTvRight(TextView tvRight) {
        this.tvRight = tvRight;
        return this;
    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public DefaultTitleBar setTvTitle(TextView tvTitle) {
        this.tvTitle = tvTitle;
        return this;
    }

}
